﻿namespace xWF.Examples.Tkmv
{
    public abstract class TkmvButtonTransitions : IWorkflowTransition<TkmvWorkItem>
    {
        public string Name { get; private set; }

        public string Title { get; set; }

        public string Icon { get; set; }

        public string Error { get; set; }

        public string Redirect { get; set; }


        protected TkmvButtonTransitions()
        {
            Name = GetType().Name;
        }

        protected TkmvButtonTransitions(string name)
        {
            Name = name;
        }

        public IWorkflowState<TkmvWorkItem> Execute(TkmvWorkItem workItem)
        {
            if (!CanExecute(workItem))
            {
                Error = "Нет разрешения на выполнение операции";
                return null;
            }

            Error = string.Empty;
            Redirect = string.Empty;

            OnExecute(workItem);

            var currentStatus = workItem.CardStatus();

            return currentStatus == TkmvStatus.None ? null : new TkmvState(currentStatus);
        }

        public abstract bool CanExecute(TkmvWorkItem workItem);

        protected abstract void OnExecute(TkmvWorkItem workItem);
    }
}
